package com.globallogic.gtalk.client;

import java.util.ArrayList;

import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class ChatListActivity extends Activity{
	
	private static final String TAG= ChatListActivity.class.getSimpleName();
	private ArrayList<String> messages = new ArrayList();
	private EditText mRecipient;
	private EditText mSendText;
	private ListView mList;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		setConnection(SharedObject.getConnection());
		mRecipient = (EditText) this.findViewById(R.id.recipient);
        mSendText = (EditText) this.findViewById(R.id.sendText);
		mList = (ListView) this.findViewById(R.id.listMessages);
		
        // Set a listener to send a chat text message
        Button send = (Button) this.findViewById(R.id.send);
        send.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                String to = mRecipient.getText().toString();
                String text = mSendText.getText().toString();

                Log.i("XMPPClient", "Sending text [" + text + "] to [" + to + "]");
                Message msg = new Message(to, Message.Type.chat);
                msg.setBody(text);
                SharedObject.getConnection().sendPacket(msg);
                messages.add(SharedObject.getConnection().getUser() + ":");
                messages.add(text);
                setListAdapter();
              
            }
        });
		
		
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}
	

	public void setConnection (XMPPConnection  connection) {
		
		if (connection != null) {
		    // Add a packet listener to get messages sent to us
		    PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
		    connection.addPacketListener(new PacketListener() {
		        public void processPacket(Packet packet) {
		            Message message = (Message) packet;
		            if (message.getBody() != null) {
		                String fromName = StringUtils.parseBareAddress(message.getFrom());
		                Log.i(TAG, "Got text [" + message.getBody() + "] from [" + fromName + "]");
		                messages.add(fromName + ":");
		                messages.add(message.getBody());
		                // Add the incoming message to the list view
		                mHandler.post(new Runnable() {
		                    public void run() {
		                        setListAdapter();
		                    }
		                });
		            }
		        }
		    }, filter);
		}
	}

    private void setListAdapter() {
	ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
	        R.layout.multi_line_list_item,
	        messages);
		mList.setAdapter(adapter);
	}
    private Handler mHandler = new Handler();	
}
